package com.huwaiwai.activemq.demo01;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * activeMq消费者
 * Created by huweiwei on 2017/4/28.
 */
public class ActiceConsumer {

    //默认连接用户名
    private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
    //默认连接密码
    private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
    //默认连接地址
    private static final String BROKEURL = ActiveMQConnection.DEFAULT_BROKER_URL;


    public static void main(String [] args){

        ConnectionFactory connectionFactory ;
        Connection connection;
        Session session;
        Destination destination;
        MessageConsumer messageConsumer;
        

        connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKEURL);

        try {
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            destination = session.createQueue("HelloWorld");
            messageConsumer = session.createConsumer(destination);

            while(true) {
                TextMessage message = (TextMessage) messageConsumer.receive(100000);
                if (message != null) {
                    System.out.println("收到的消息：" + message.getText());
                } else {
                    break;
                }
            }
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}
